package com.xm.httpapi.Aspect;

import android.os.Looper;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

@Aspect
public class MainAspect {
    private static final String POINTCUT_METHOD = "execution(@com.xm.httpapi.Annotation.Main * *(..))";


    @Around("onMainMethod()")
    public void doMainMethod(final ProceedingJoinPoint joinPoint) throws Throwable {
        mainMethod(joinPoint);
    }

    @Pointcut(POINTCUT_METHOD)
    public void onMainMethod() { }

    private void mainMethod(final ProceedingJoinPoint joinPoint) throws Throwable {
        Flowable.create(e -> {
            {
                try {
                    joinPoint.proceed();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        }, BackpressureStrategy.BUFFER)
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();
    }
}
